package com.yserp.kp.service.impl;
import com.yserp.kp.service.TKpYushouHServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import com.yserp.kp.entity.TKpYushouHEntity;
import com.yserp.kp.entity.TKpYushouEntity;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import java.util.ArrayList;
import java.util.UUID;
import java.io.Serializable;


@Service("tKpYushouHService")
@Transactional
public class TKpYushouHServiceImpl extends CommonServiceImpl implements TKpYushouHServiceI {
	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((TKpYushouHEntity)entity);
 	}
	
	public void addMain(TKpYushouHEntity tKpYushouH,
	        List<TKpYushouEntity> tKpYushouList){
			//保存主信息
			this.save(tKpYushouH);
		
			/**保存-收款行项目*/
			for(TKpYushouEntity tKpYushou:tKpYushouList){
				//外键设置
				tKpYushou.setSkid(tKpYushouH.getSkid());
				tKpYushou.setCusCode(tKpYushouH.getCusCode());
				tKpYushou.setCusName(tKpYushouH.getCusName());
				tKpYushou.setGdCode(tKpYushouH.getGdCode());
				tKpYushou.setGdName(tKpYushouH.getGdName());
				tKpYushou.setGdDaaima(tKpYushouH.getGdDaima());
				tKpYushou.setYsType(tKpYushouH.getYsType());
				try{
					double ysjine = 0.00;
					if("退款".equals(tKpYushou.getYsType())){
						ysjine=0-Math.abs(Double.parseDouble(tKpYushou.getYsJine()));
						tKpYushou.setYsJine(Double.toString(ysjine));
					}
				}catch (Exception e){

				}

				this.save(tKpYushou);
			}
			//执行新增操作配置的sql增强
 			this.doAddSql(tKpYushouH);
	}

	
	public void updateMain(TKpYushouHEntity tKpYushouH,
	        List<TKpYushouEntity> tKpYushouList) {
		//保存主表信息
		if(StringUtil.isNotEmpty(tKpYushouH.getId())){
			try {
				TKpYushouHEntity temp = findUniqueByProperty(TKpYushouHEntity.class, "id", tKpYushouH.getId());
				MyBeanUtils.copyBeanNotNull2Bean(tKpYushouH, temp);
				this.saveOrUpdate(temp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			this.saveOrUpdate(tKpYushouH);
		}
		//===================================================================================
		//获取参数
		Object id0 = tKpYushouH.getSkid();
		//===================================================================================
		//1.查询出数据库的明细数据-收款行项目
	    String hql0 = "from TKpYushouEntity where 1 = 1 AND sKID = ? ";
	    List<TKpYushouEntity> tKpYushouOldList = this.findHql(hql0,id0);
		//2.筛选更新明细数据-收款行项目
		if(tKpYushouList!=null&&tKpYushouList.size()>0){
		for(TKpYushouEntity oldE:tKpYushouOldList){
			boolean isUpdate = false;
				for(TKpYushouEntity sendE:tKpYushouList){
					//需要更新的明细数据-收款行项目
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-收款行项目
		    		super.delete(oldE);
	    		}
	    		
			}
			//3.持久化新增的数据-收款行项目
			for(TKpYushouEntity tKpYushou:tKpYushouList){
				if(oConvertUtils.isEmpty(tKpYushou.getId())){
					//外键设置
					tKpYushou.setSkid(tKpYushouH.getSkid());
					this.save(tKpYushou);
				}
			}
		}
		//执行更新操作配置的sql增强
 		this.doUpdateSql(tKpYushouH);
	}

	
	public void delMain(TKpYushouHEntity tKpYushouH) {
		//删除主表信息
		this.delete(tKpYushouH);
		//===================================================================================
		//获取参数
		Object id0 = tKpYushouH.getSkid();
		//===================================================================================
		//删除-收款行项目
	    String hql0 = "from TKpYushouEntity where 1 = 1 AND sKID = ? ";
	    List<TKpYushouEntity> tKpYushouOldList = this.findHql(hql0,id0);
		this.deleteAllEntitie(tKpYushouOldList);
	}
	
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @return
	 */
 	public boolean doAddSql(TKpYushouHEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @return
	 */
 	public boolean doUpdateSql(TKpYushouHEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @return
	 */
 	public boolean doDelSql(TKpYushouHEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,TKpYushouHEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{sys_org_code}",String.valueOf(t.getSysOrgCode()));
 		sql  = sql.replace("#{sys_company_code}",String.valueOf(t.getSysCompanyCode()));
 		sql  = sql.replace("#{bpm_status}",String.valueOf(t.getBpmStatus()));
 		sql  = sql.replace("#{cus_code}",String.valueOf(t.getCusCode()));
 		sql  = sql.replace("#{cus_name}",String.valueOf(t.getCusName()));
 		sql  = sql.replace("#{gd_code}",String.valueOf(t.getGdCode()));
 		sql  = sql.replace("#{gd_name}",String.valueOf(t.getGdName()));
 		sql  = sql.replace("#{ys_type}",String.valueOf(t.getYsType()));
 		sql  = sql.replace("#{gd_daima}",String.valueOf(t.getGdDaima()));
 		sql  = sql.replace("#{sk_amount}",String.valueOf(t.getSkAmount()));
 		sql  = sql.replace("#{skid}",String.valueOf(t.getSkid()));
 		sql  = sql.replace("#{sk_remark}",String.valueOf(t.getSkRemark()));
 		sql  = sql.replace("#{by1}",String.valueOf(t.getBy1()));
 		sql  = sql.replace("#{by2}",String.valueOf(t.getBy2()));
 		sql  = sql.replace("#{by3}",String.valueOf(t.getBy3()));
 		sql  = sql.replace("#{by4}",String.valueOf(t.getBy4()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
}